package com.vue.controller;

import com.alibaba.fastjson.JSON;
import com.vue.entitycm.User;
import com.vue.service.VueService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2018/6/11.
 */
@Controller
public class VueController {

    @Resource
    private VueService vueService;

    @RequestMapping("/index")
    public String getIndex() throws Exception {
        return "index";
    }


    @RequestMapping("/getuser/{id}")
    @ResponseBody
    public String getUser(@PathVariable String id) throws Exception {

        User user = vueService.getUser(id);
        System.out.println(JSON.toJSONString(user));
        return JSON.toJSONString(user);

    }

    @RequestMapping("/getusers")
    public String getUsers() throws Exception {

        List<User> users = vueService.getUser();
        System.out.println(JSON.toJSONString(users));
        return "";

    }

    @RequestMapping("/getmessage")
    @ResponseBody
    public String getMessages() throws Exception {
        List messages = vueService.getMessages();
        System.out.println(JSON.toJSONString(messages));
        return JSON.toJSONString(messages);
    }

    /**
     * @ResponseBody 是返回对象，不加这个注解string 就是返回路径，使用model带参数
     * 示例如下：
     *
     */
    /*@RequestMapping("/item/{itemId}")
    public String showItemInfo(@PathVariable Long itemId, Model model) {

        TbItem tbItem = itemService.getItemById(itemId);
        TbItemDesc itemDesc = itemService.getItemDescById(itemId);

        Item item = new Item(tbItem);
        model.addAttribute("item",item);
        model.addAttribute("itemDesc",itemDesc);

        return "item";

    }*/

}
